<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/11/25
 * Time: 10:45
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\ShortVideoService;
use App\Constant\ErrorConstant;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ShortVideoController extends BaseController
{
    protected $service;

    public function __construct(Request $request, ShortVideoService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 短视频列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listVideo()
    {
        $page   =   (int) $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->videoList($page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_VIDEO_LIST_ERROR, $result);
        }
    }

    /**
     * 短视频页面
     * @return \Illuminate\Http\JsonResponse
     */
    public function pageVideo()
    {
        [$flag, $result]    =   $this->service->videoPage();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_VIDEO_LIST_ERROR, $result);
        }
    }

    /**
     * 短视频详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailVideo()
    {
        $validator  =   Validator::make($this->request->post(), [
            'video_id'  =>  'required|integer|exists:mini_short_video,id'
        ], [
            'video_id.required' =>  '短视频ID不能为空',
            'video_id.integer'  =>  '短视频ID必须为整型',
            'video_id.exists'   =>  '短视频不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_VIDEO_DETAIL_ERROR, $validator->errors()->first());
        }

        $video_id   =   (int) $this->request->post('video_id');
        [$flag, $result]    =   $this->service->videoDetail($video_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_VIDEO_DETAIL_ERROR, $result);
        }
    }

    /**
     * 点击短视频
     * @return \Illuminate\Http\JsonResponse
     */
    public function clickVideo()
    {
        $validator  =   Validator::make($this->request->post(), [
            'video_id'  =>  'required|integer|exists:mini_short_video,id'
        ], [
            'video_id.required' =>  '短视频ID不能为空',
            'video_id.integer'  =>  '短视频ID必须为整型',
            'video_id.exists'   =>  '短视频不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_VIDEO_CLICK_ERROR, $validator->errors()->first());
        }

        $video_id   =   (int) $this->request->post('video_id');
        [$flag, $result]    =   $this->service->videoClick($video_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_VIDEO_CLICK_ERROR, $result);
        }
    }

    /**
     * 短视频加购
     * @return \Illuminate\Http\JsonResponse
     */
    public function cartVideo()
    {
        $validator  =   Validator::make($this->request->post(), [
            'video_id'  =>  'required|integer|exists:mini_short_video,id'
        ], [
            'video_id.required' =>  '短视频ID不能为空',
            'video_id.integer'  =>  '短视频ID必须为整型',
            'video_id.exists'   =>  '短视频不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_VIDEO_CLICK_ERROR, $validator->errors()->first());
        }

        $video_id   =   (int) $this->request->post('video_id');
        [$flag, $result]    =   $this->service->videoCart($video_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_VIDEO_CLICK_ERROR, $result);
        }
    }

    /**
     * 短视频收藏
     * @return \Illuminate\Http\JsonResponse
     */
    public function collectVideo()
    {
        $validator  =   Validator::make($this->request->post(), [
            'video_id'  =>  'required|integer|exists:mini_short_video,id'
        ], [
            'video_id.required' =>  '短视频ID不能为空',
            'video_id.integer'  =>  '短视频ID必须为整型',
            'video_id.exists'   =>  '短视频不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_VIDEO_CLICK_ERROR, $validator->errors()->first());
        }

        $video_id   =   (int) $this->request->post('video_id');
        [$flag, $result]    =   $this->service->videoCollect($video_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_VIDEO_CLICK_ERROR, $result);
        }
    }

    /**
     * 短视频播放
     * @return \Illuminate\Http\JsonResponse
     */
    public function playVideo()
    {
        $validator  =   Validator::make($this->request->post(), [
            'video_id'  =>  'required|integer|exists:mini_short_video,id'
        ], [
            'video_id.required' =>  '短视频ID不能为空',
            'video_id.integer'  =>  '短视频ID必须为整型',
            'video_id.exists'   =>  '短视频不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_VIDEO_CLICK_ERROR, $validator->errors()->first());
        }

        $video_id   =   (int) $this->request->post('video_id');
        [$flag, $result]    =   $this->service->videoPlay($video_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_VIDEO_CLICK_ERROR, $result);
        }
    }
}
